package pro.zfb.service.zfb.follow.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.xiaoleilu.hutool.json.JSONObject;

import com.yy.kit.array.YYArrayKits;
import com.yy.kit.log.YYLogKits;
import com.yy.kit.string.YYStrKits;
import com.yy.kit.zfb.constans.AlipayConstants;
import pro.zfb.entity.TZfbFollow;
import pro.zfb.entity.TZfbFollowExample;
import pro.zfb.mapper.TZfbFollowMapper;
import pro.zfb.service.zfb.follow.IZFBFollowService;

@Service(value = "iZFBFollowService")
public class ZFBFollowServiceImpl implements IZFBFollowService {

    private final TZfbFollowMapper tZfbFollowMapper;

    @Autowired
    public ZFBFollowServiceImpl(TZfbFollowMapper tZfbFollowMapper) {
        this.tZfbFollowMapper = tZfbFollowMapper;
    }

    @Override
    public String floow(JSONObject biz_content) {
        //1. 获取用户openid
        String FromAlipayUserId = biz_content.getStr(AlipayConstants.FROMUSER);
        String user_name = biz_content.getJSONObject("UserInfo").getStr("user_name");
        String logon_id = biz_content.getJSONObject("UserInfo").getStr("logon_id");

        YYLogKits.info(IZFBFollowService.class, "├用户openid:{}关注了支付宝!", FromAlipayUserId);

        //2. 查询用户是否已经在关注表中
        TZfbFollowExample example = new TZfbFollowExample();
        example.createCriteria().andOpenidEqualTo(FromAlipayUserId);
        long count = tZfbFollowMapper.countByExample(example);
        if (count > 0) {//已经在表中，那么更新
            //3.获取表中的对象
            List<TZfbFollow> followList = tZfbFollowMapper.selectByExample(example);
            if (YYArrayKits.isNotEmpty(followList)) {
                TZfbFollow follow = followList.get(0);
                follow.setFollow_status(1);
                follow.setFollow_date(new Date());
                follow.setFollow_count(follow.getFollow_count() + 1);
                tZfbFollowMapper.updateByPrimaryKey(follow);
            }
        } else {//不在表中，那么插入
            //4.用户新关注，插入到关注表
            TZfbFollow follow = new TZfbFollow();
            follow.setId(YYStrKits.getTableId());
            follow.setOpenid(FromAlipayUserId);
            follow.setFollow_status(1);
            follow.setFollow_count(1);
            follow.setFollow_date(new Date());
            follow.setLogon_id(logon_id);
            follow.setUser_name(user_name);
            tZfbFollowMapper.insert(follow);
        }
        return "";
    }

    @Override
    public String unfloow(JSONObject biz_content) {
        //1. 获取用户openid
        String FromAlipayUserId = biz_content.getStr(AlipayConstants.FROMUSER);

        YYLogKits.info(IZFBFollowService.class, "├用户openid:{}取消关注了支付宝!", FromAlipayUserId);

        //2. 更新用户状态
        TZfbFollowExample example = new TZfbFollowExample();
        example.createCriteria().andOpenidEqualTo(FromAlipayUserId);
        List<TZfbFollow> followList = tZfbFollowMapper.selectByExample(example);
        if (YYArrayKits.isNotEmpty(followList)) {
            TZfbFollow follow = followList.get(0);
            follow.setFollow_status(0);
            follow.setUnfollow_date(new Date());
            tZfbFollowMapper.updateByPrimaryKey(follow);
        }
        return "";
    }

}
